package sd.zhr.moodcapsule.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import sd.zhr.moodcapsule.common.WrapMapper;
import sd.zhr.moodcapsule.common.Wrapper;
import sd.zhr.moodcapsule.pojo.MoodComment;
import sd.zhr.moodcapsule.service.MoodCommentServiceImpl;

import java.util.List;

@RestController
@RequestMapping("/moodcomment") // 指定基础路径
@CrossOrigin //
public class MoodCommentController {
    @Autowired
    private MoodCommentServiceImpl moodCommentService;
    @PostMapping("/getAllcommentByMoodId")
    @Secured({"ROLE_ADMIN", "ROLE_COMMON"})
    public Wrapper<List<MoodComment>> getMoodCommentsById(int moodId) {
        List<MoodComment> moodCommentList=moodCommentService.getMoodComments(moodId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, moodCommentList);
    }
    @PostMapping("/add")
    @Secured({"ROLE_ADMIN", "ROLE_COMMON"})
    public Wrapper<Boolean> addMoodComment(@RequestBody MoodComment moodComment) {
        System.out.println(moodComment);
        int result=moodCommentService.addMoodComment(moodComment);
        if (result>0) {
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, true);
        }else {
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作失败", false);
        }
    }
    @PutMapping("/update")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<Boolean> updateMoodComment(@RequestBody MoodComment moodComment) {
        int result=moodCommentService.updateMoodComment(moodComment);
        if (result>0) {
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, true);
        }
        else {
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作失败", false);
        }
    }
    @DeleteMapping("/delete/{id}")
    @Secured({"ROLE_ADMIN", "ROLE_COMMON"})
    public Wrapper<Boolean> deleteMoodComment(@PathVariable Integer id) {
        int result=moodCommentService.deleteMoodComment(id);
        if (result>0) {
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, true);
        }else {
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作失败", false);
        }
    }
}
